package org.emergency.framework.action.oa;

import java.io.ByteArrayInputStream;

import javax.annotation.Resource;

import org.apache.struts2.json.annotations.JSON;
import org.dgm.dgmJar.propertyUtil.DCommProperty;
import org.emergency.framework.action.BaseAction;
import org.emergency.framework.bean.PageHelper;
import org.emergency.framework.common.Context;
import org.emergency.framework.common.UserSession;
import org.emergency.framework.constant.LogLocal;
import org.emergency.framework.constant.SessionConstant;
import org.emergency.framework.constant.SysConfConstant;
import org.emergency.framework.model.TbUser;
import org.emergency.framework.otherDatasourse.ITbSysConfigDAO;
import org.emergency.framework.service.oa.ITbUserService;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

@Repository("tbUserAction")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class TbUserAction extends BaseAction {
	private static final long serialVersionUID = -6546504159764732917L;

	private String result;

	
	private PageHelper pageHelper;
	
	/**
	 * 用户
	 */
	private TbUser tbUser;
	/**
	 * 角色id
	 */
	private String roleId;

	/**
	 * 部门id
	 */
	private String deptId;

	/**
	 * 部门id的集合string,用来传一堆部门的,中间用,分隔
	 */
	private String deptString;

	/**
	 * 角色的集合string,用来传一堆角色的,中间用,分隔
	 */
	private String roleString;

	/**
	 * 服务
	 */
	@Resource(name = "tbUserServiceImpl")
	private ITbUserService service;

	/**
	 * 图片流
	 */
	private ByteArrayInputStream inputStream;

	/**
	 * 验证码
	 */
	private String verifyCode;

	/**
	 * userSession
	 */
	private UserSession userSession;

	/**
	 * 生成验证码
	 * 
	 * @return
	 * @throws Exception
	 */
	public String getImage() throws Exception {
		try {
			setResult("得到图片成功");
			inputStream = service.getImage();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "getImage";
	}

	public String queryUserById() {
		setResult("查询成功");

		try {
			tbUser = service.queryUserById(tbUser);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			setResult("查询失败");
		}

		return "queryUserById";
	}

	/**
	 * 得到userSession
	 * 
	 * @return
	 */
	public String getUserSession() {

		userSession = (UserSession) Context.getSession().getAttribute(
				SessionConstant.UserSession);

		return "getUserSession";
	}

	/**
	 * 获得当前的用户
	 * 
	 * @return
	 */
	public String findCurrentUser() {

		userSession = (UserSession) Context.getSession().getAttribute(
				SessionConstant.UserSession);
		TbUser tbUser = userSession.getTbUser();

		return "findCurrentUser";
	}


	public void setUserSession(UserSession userSession) {
		this.userSession = userSession;
	}

	public void setInputStream(ByteArrayInputStream inputStream) {
		this.inputStream = inputStream;
	}

	@JSON(deserialize=false)
	public ByteArrayInputStream getInputStream() {
		return inputStream;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String message) {
		this.result = message;
	}

	@JSON(deserialize=false)
	public String getVerifyCode() {
		return verifyCode;
	}

	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}

	public TbUser getTbUser() {
		return tbUser;
	}

	public void setTbUser(TbUser tbUser) {
		this.tbUser = tbUser;
	}

	@JSON(deserialize=false)
	public ITbUserService getService() {
		return service;
	}

	public void setService(ITbUserService service) {
		this.service = service;
	}


	public PageHelper getPageHelper() {
		return pageHelper;
	}

	public void setPageHelper(PageHelper pageHelper) {
		this.pageHelper = pageHelper;
	}

	/**
	 * 管理员增加用户
	 * 
	 * @return
	 */
	public String addUser() {
		setResult("增加成功");
		try {
			service.save(tbUser, deptId, roleId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			setResult("增加失败");
			e.printStackTrace();
		}
		return "addUser";
	}

	/**
	 * 管理员修改用户
	 * 
	 * @return
	 */
	public String editUser() {
		setResult("修改成功");
		try {
			service.editUser(tbUser, deptId, roleId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			setResult("修改失败");
			e.printStackTrace();
		}
		return "editUser";
	}

	/**
	 * 个人信息修改
	 * 
	 * @return
	 */
	public String editCurrentUser() {
		setResult("修改成功");
		try {
			service.editCurrentUser(tbUser);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			setResult("修改失败");
		}
		return "editCurrentUser";
	}

	@Resource(name = "tbSysConfigDAOImpl")
	private ITbSysConfigDAO tbSysConfigDAO;

	/**
	 * 用户登陆服务
	 * 
	 * @return
	 */
	public String checkLogin() {
		if (!verifyCode.equals(Context.getSession().getAttribute(
				SessionConstant.verifyCode))) { // 验证吗是否正确
			// 这里为了测试，把验证码错误给注了
			// result = "验证码错误";
			// return "checkLogin";
		}
		if (!service.checkLogin(tbUser)) {
			result = "登陆失败，请检查用户名和密码";
			return "checkLogin";
		}
		result = "success";

		/** 以下是测试第二个datasourse的代码 */
		System.out.println(SysConfConstant.getAnnex_pathsysconfig().getTitle()
				+ "8******");
		return "checkLogin"; // 成功
	}

	/**
	 * 分页查询用户
	 * 
	 * @return
	 */
	public String queryTbUser() {
		setResult("查询成功");
		try {
			// 设置最大页,数据来源为CustomConfig.properties
			pageHelper.setItemNum(Integer.parseInt(DCommProperty
					.getPropertyInstance("/CustomConfig.properties")
					.getProperty("usersJspmaxRow")));

			LogLocal.loggerOA.info(pageHelper);
			pageHelper = service.queryUser(pageHelper, roleId, tbUser,
					deptId);
			LogLocal.loggerOA.debug("这是一个测试");
			LogLocal.loggerOA.debug(pageHelper);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			setResult("查询失败");
			e.printStackTrace();
		}
		//List<TbUser> tbUsers = pageHelper.getList();
		/*
		 * for (TbUser tbUser : tbUsers) { System.out.println(tbUser.getName()); }
		 */

		return "queryTbUser";
	}

	/**
	 * 得到当前user
	 * 
	 * @return
	 */
	public String queryCurrentUser() {
		userSession = (UserSession) Context.getSession().getAttribute(
				SessionConstant.UserSession);
		tbUser = userSession.getTbUser();
		return "queryCurrentUser";
	}

	public String deleteUserById() {
		setResult("删除成功");
		try {
			service.deleteUserById(tbUser);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			setResult("删除失败");
		}
		return "deleteUserById";
	}

	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	
	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public String getDeptString() {
		return deptString;
	}

	public void setDeptString(String deptString) {
		this.deptString = deptString;
	}

	public String getRoleString() {
		return roleString;
	}

	public void setRoleString(String roleString) {
		this.roleString = roleString;
	}

	@JSON(deserialize=false)
	public ITbSysConfigDAO getTbSysConfigDAO() {
		return tbSysConfigDAO;
	}

	public void setTbSysConfigDAO(ITbSysConfigDAO tbSysConfigDAO) {
		this.tbSysConfigDAO = tbSysConfigDAO;
	}

}
